package com.youlu.campus.admin.operator;

import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.OperatorTask;
import com.youlu.campus.entity.VO.req.OperatorTaskQueryVO;
import com.youlu.campus.service.operator.OperatorTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Slf4j
@RestController
public class OperatorTaskController {
    @Autowired
    private OperatorTaskService operatorTaskService;

    @PostMapping("/operator-task/create")
    public RestResult create(@RequestBody @Valid OperatorTask req) {
        String comId = AuthUtils.getComId();
        req.setOptUser(AuthUtils.getLoginName());
        req.setTenantId(comId);
        boolean re = operatorTaskService.create(req);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/operator-task/update")
    public RestResult update(@RequestBody @Valid OperatorTask req) {
        boolean re = operatorTaskService.update(req);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/operator-task/list")
    public RestResult list(@RequestBody @Valid OperatorTaskQueryVO req) {
        req.setOptUser(AuthUtils.getLoginName());
        return RestResultBuilder.builder().success(operatorTaskService.list(req)).build();
    }

    @GetMapping("/operator-task/find-one")
    public RestResult findOne(@RequestParam("id") @NotNull String id) {
        return RestResultBuilder.builder().success(operatorTaskService.findOne(id)).build();
    }

    @GetMapping("/operator-task/start")
    public RestResult start(@RequestParam("id") @NotNull String id) {
        return RestResultBuilder.builder().success(operatorTaskService.start(id)).build();
    }
    @GetMapping("/operator-task/delete")
    public RestResult delete(@RequestParam("id") @NotNull String id) {
        return RestResultBuilder.builder().success(operatorTaskService.delete(id)).build();
    }
}
